import pandas as pd
import numpy as np
import pickle
import sys

# write out results
sys.stdout = open("/REDACTED/share_cost_revenue_by_model.txt", "w")

# read in csv's outputted at 1.45% budget during model training, calculate weighted share of those audited in activity code 271

print("Share with Substantial Business Income (ref reg, reg, ref oracle, oracle)")
ref_reg = pd.read_csv('/REDACTED/data/modeled_refactor_temp/unres_reg_selected_taxpayer_ids_alt_outcome.csv')
ref_reg_aud = ref_reg[ref_reg.aud_ind == 1]
ref_reg_aud['yes_271'] = np.where(ref_reg_aud.activity_code == 271, 1, 0)
ref_reg_aud['wgt_271'] = ref_reg_aud['yes_271'] * ref_reg_aud['base_weight']

round(ref_reg_aud.wgt_271.sum() / ref_reg_aud.base_weight.sum(), 3)

reg = pd.read_csv('/REDACTED/data/modeled_refactor_temp/unres_reg_selected_taxpayer_ids.csv')
reg_aud = reg[reg.aud_ind == 1]
reg_aud['yes_271'] = np.where(reg_aud.activity_code == 271, 1, 0)
reg_aud['wgt_271'] = reg_aud['yes_271'] * reg_aud['base_weight']

round(reg_aud.wgt_271.sum() / reg_aud.base_weight.sum(), 3)

ref_oracle = pd.read_csv('/REDACTED/data/modeled_refactor_temp/unres_oracle_selected_taxpayer_ids_alt_outcome.csv')
ref_oracle_aud = ref_oracle[ref_oracle.aud_ind == 1]
ref_oracle_aud['yes_271'] = np.where(ref_oracle_aud.activity_code == 271, 1, 0)
ref_oracle_aud['wgt_271'] = ref_oracle_aud['yes_271'] * ref_oracle_aud['base_weight']

round(ref_oracle_aud.wgt_271.sum() / ref_oracle_aud.base_weight.sum(), 3)

oracle = pd.read_csv('/REDACTED/data/modeled_refactor_temp/unres_oracle_selected_taxpayer_ids.csv')
oracle_aud = oracle[oracle.aud_ind == 1]
oracle_aud['yes_271'] = np.where(oracle_aud.activity_code == 271, 1, 0)
oracle_aud['wgt_271'] = oracle_aud['yes_271'] * oracle_aud['base_weight']

round(oracle_aud.wgt_271.sum() / oracle_aud.base_weight.sum(), 3)


### Costs and Revenues
# read in plot dictionaries using original (operational) costs
print('\nOperational Costs (Millions)(ref reg, reg, ref oracle, oracle)')
defaultaxpayer_id= '/REDACTED/data/modeled_refactor_temp/'

### Refundable Credit - REGRESSOR
with open(defaultaxpayer_id + 'eitc_unres_reg_plus_dep_database_output_outcome_ref_cred_amt_dif_pv_new_bifsg.pickle', 'rb') as f:
    ref_reg_dict = pickle.load(f)

### Regressor
with open(defaultaxpayer_id + 'eitc_unres_reg_plus_dep_database_output_new_bifsg.pickle', 'rb') as f:
    reg_dict = pickle.load(f)

### Overclaiming Oracle
with open(defaultaxpayer_id + 'eitc_unres_oracle_plus_dep_database_output_outcome_ref_cred_amt_dif_pv_new_bifsg.pickle', 'rb') as f:
    ref_oracle_dict = pickle.load(f)

### Oracle
with open(defaultaxpayer_id + 'eitc_unres_oracle_plus_dep_database_output_new_bifsg.pickle', 'rb') as f:
    oracle_dict = pickle.load(f)

# access costs at 1.45% budget
ref_reg_cost = float(ref_reg_dict['1.45p_mean_5_fold_cost_unres_reg'][0])
reg_cost = float(reg_dict['1.45p_mean_5_fold_cost_unres_reg'][0])
ref_oracle_cost = float(ref_oracle_dict['1.45p_mean_5_fold_cost_unres_oracle'][0])
oracle_cost = float(oracle_dict['1.45p_mean_5_fold_cost_unres_oracle'][0])

round(ref_reg_cost/1000000)
round(reg_cost/1000000)
round(ref_oracle_cost/1000000)
round(oracle_cost/1000000)

## research_audits costs
# read in plot dictionaries using new (research_audits) costs
print('\nresearch_audits Costs (Millions)(ref reg, reg, ref oracle, oracle)')
defaultaxpayer_id= '/REDACTED/data/modeled_refactor_temp/costs_v2/'

### Refundable Credit - REGRESSOR

with open(defaultaxpayer_id + 'eitc_unres_reg_plus_dep_database_output_outcome_ref_cred_amt_dif_pv_new_bifsg.pickle', 'rb') as f:
    ref_reg_v2_dict = pickle.load(f)

### Regressor

with open(defaultaxpayer_id + 'eitc_unres_reg_plus_dep_database_output_new_bifsg.pickle', 'rb') as f:
    reg_v2_dict = pickle.load(f)

### Overclaiming Oracle

with open(defaultaxpayer_id + 'eitc_unres_oracle_plus_dep_database_output_outcome_ref_cred_amt_dif_pv_new_bifsg.pickle', 'rb') as f:
    ref_oracle_v2_dict = pickle.load(f)

### Oracle

with open(defaultaxpayer_id + 'eitc_unres_oracle_plus_dep_database_output_new_bifsg.pickle', 'rb') as f:
    oracle_v2_dict = pickle.load(f)

# access costs at 1.45% budget
ref_reg_v2_cost = float(ref_reg_v2_dict['1.45p_mean_5_fold_cost_unres_reg'][0])
reg_v2_cost = float(reg_v2_dict['1.45p_mean_5_fold_cost_unres_reg'][0])
ref_oracle_v2_cost = float(ref_oracle_v2_dict['1.45p_mean_5_fold_cost_unres_oracle'][0])
oracle_v2_cost = float(oracle_v2_dict['1.45p_mean_5_fold_cost_unres_oracle'][0])

round(ref_reg_v2_cost/1000000)
round(reg_v2_cost/1000000)
round(ref_oracle_v2_cost/1000000)
round(oracle_v2_cost/1000000)

# access revenues at 1.45% budget
print('\nDetected Underreportaxpayer_idg (Millions)(ref reg, reg, ref oracle, oracle)')
ref_reg_v2_revenue = float(ref_reg_v2_dict['1.45p_mean_5_fold_revenue_unres_reg'][0])
reg_v2_revenue = float(reg_v2_dict['1.45p_mean_5_fold_revenue_unres_reg'][0])
ref_oracle_v2_revenue = float(ref_oracle_v2_dict['1.45p_mean_5_fold_revenue_unres_oracle'][0])
oracle_v2_revenue = float(oracle_v2_dict['1.45p_mean_5_fold_revenue_unres_oracle'][0])

round(ref_reg_v2_revenue/1000000)
round(reg_v2_revenue/1000000)
round(ref_oracle_v2_revenue/1000000)
round(oracle_v2_revenue/1000000)

sys.stdout = sys.__stdout__

# confirmed these revenues are the same as 5 fold revenue in original dicts
# we grab the dict code from model_costs_revenues.py